﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShoppingStore.Models;
namespace ShoppingStore.Areas.Admin.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Admin/Home/

        EStoreDbContext db = new EStoreDbContext();
        [ValidateInput (false)]
        public ActionResult Index(Product model,string command=null)
        {
            var file = Request.Files["uplImage"];
            if (file != null && file.ContentLength > 0)
            {
                file.SaveAs( Server.MapPath("~/Images/Products/" + file.FileName));
                model.Image = file.FileName;


            }

            else if (command =="insert") {
                db.Products.Add(model);
                db.SaveChanges();

            }
            else if (command == "update") {

                db.Entry(model).State = System.Data.EntityState.Modified;
                db.SaveChanges();

            }
            else if (command == "delete") {

                if (User.IsInRole("Admin"))
                {
                    model = db.Products.Find(model.Id);
                    db.Products.Remove(model);
                    db.SaveChanges();
                    ModelState.AddModelError("", "Bạn đã xóa thành công");
                }
                else {
                    ModelState.AddModelError("", "Bạn Không có quyền xóa");

                }
            }
            else if (command == "clear") {
                return RedirectToAction("Index");
            }
            else if (command == "edit") {
                model = db.Products.Find(model.Id);

            }
            else if (command == "seach")
            {
                Session["supplierId"] = model.SupplierId;
                Session["categoryId"] = model.CategoryId;

                if (Session["categoryId"] == null) {
                    Session["categoryId"] = 0;
                }

                var categoryId = (int)Session["categoryId"];
                var supplierId = (string)Session["supplierId"];
                if (string.IsNullOrEmpty(supplierId) && categoryId == 0) {
                    ViewBag.Products = db.Products;

                }
                if (categoryId == 0) {
                    ViewBag.Products = db.Products.Where(c => c.SupplierId == supplierId);
                }
                if (supplierId == null) {
                    ViewBag.Products = db.Products.Where(p => p.CategoryId == categoryId);
                }
                if(categoryId!=0 && supplierId!=null){
                    ViewBag.Products = db.Products.
                        Where(p => p.CategoryId == categoryId && p.SupplierId == supplierId);

                }



            }
            ViewBag.CategoryId = new SelectList(db.Categories, "Id", "Name", model.CategoryId);
            ViewBag.SupplierId = new SelectList(db.Suppliers, "Id", "Name", model.SupplierId);

            return View(model);
        }

    }
}
